package org.come.Jpanel;

import com.tool.btn.TitlelBtn;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.List;

public class CustomTitleBarUI3 extends JPanel {
    private JLabel titleLabel;
    private TitlelBtn b1, b2;
    private List<TitlelBtn> titlelBtns;
    private String title;
    private Image myimage;
    private Integer index = 0;

    private int first_x;
    private int first_y;

    Boolean b = false;

    public CustomTitleBarUI3(JFrame frame) {

        setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        setPreferredSize(new Dimension(5, 5));
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); // 设置鼠标样式为手型


        addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {


            }

            @Override
            public void mousePressed(MouseEvent e) {
                first_x = e.getX();
                first_y = e.getY();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseMoved(final MouseEvent e) {
//
            }


            @Override
            public void mouseDragged(final MouseEvent e) {
                final int x = e.getX() - first_x;
                final int y = e.getY() - first_y;
                frame.setBounds(x + frame.getX(), y + frame.getY(), frame.getWidth(), frame.getHeight());
            }
        });


    }


    @Override
    protected void paintComponent(Graphics g) {
//        this.setBorder(BorderFactory.createLineBorder(Color.YELLOW));
        Graphics2D g2d = (Graphics2D) g;
        Color color = new Color(113, 154, 194);
        Color color1 = new Color(113, 154, 194);
//        GradientPaint gradient = new GradientPaint(0, 0, color, 0, getHeight() - 1, color1);
//        g2d.setPaint(gradient);
        g.setColor(color);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.setComposite((Composite) AlphaComposite.getInstance(1, 1));
        g2d.setColor(Color.black);
        g2d.fillRect(0, -4, 1, getHeight() );
    }

    public void updateTitle(String newTitle) {
        titleLabel.setText(newTitle);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Custom Title Bar UI Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
//        frame.setUndecorated(true);
        CustomTitleBarUI3 customTitleBarUI = new CustomTitleBarUI3(frame);
        customTitleBarUI.setOpaque(false);
        frame.add(customTitleBarUI, BorderLayout.NORTH);

        frame.setVisible(true);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }
}

